package com.wzapp.sitposture.usercenter.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wzapp.sitposture.common.page.CommonPage;
import com.wzapp.sitposture.common.page.PageParam;
import com.wzapp.sitposture.common.util.BigDecimalUtil;
import com.wzapp.sitposture.common.util.DateUtil;
import com.wzapp.sitposture.common.util.PageUtil;
import com.wzapp.sitposture.usercenter.entity.EntityUtils;
import com.wzapp.sitposture.usercenter.entity.UserSettingPosture;
import com.wzapp.sitposture.usercenter.repository.UserSettingPostureRepository;
import com.wzapp.sitposture.usercenter.service.UserSettingPostureService;
import com.wzapp.sitposture.usercenter.view.SettingPostureDTO;
import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 功能: ${DESCRIPTION}<br/>
 *
 * @author qianglong
 * @version 1.1
 * @create 2019-01-27 16:37
 **/
@Slf4j
@Service
public class UserSettingPostureServiceImpl implements UserSettingPostureService {

    @Autowired
    UserSettingPostureRepository userSettingPostureRepository;

    @Override
    public UserSettingPosture save(UserSettingPosture userSettingPosture) {

        userSettingPosture.setCreateTime(System.currentTimeMillis());

        return userSettingPostureRepository.saveAndFlush(userSettingPosture);
    }

    @Override
    public void batchSave(List<UserSettingPosture> userSettingPostures) {

        userSettingPostureRepository.save(userSettingPostures);
    }

    @Override
    public CommonPage<UserSettingPosture> findByStartTime(String userId,Long startTime,PageParam pageParam) {

        Sort sort = new Sort(Sort.Direction.ASC, "uploadTime");
        Pageable pageable = PageUtil.convertToPageable(pageParam,sort);
        Long uploadTime = startTime - 30 * 60 * 1000L;
        Page<UserSettingPosture> page = userSettingPostureRepository.findByUserIdAndUploadTimeGreaterThanEqual(userId,
                uploadTime,pageable);

        CommonPage<UserSettingPosture> commonPage = new CommonPage<>(page);
        Long learningDuration = 0L;
        for (UserSettingPosture posture : commonPage.getContent()){
            learningDuration = learningDuration + posture.getDuration();
        }

        //学习时长
        JSONObject statistics = new JSONObject();
        statistics.put("learningDuration",learningDuration);
        commonPage.setStatistics(statistics);
        return commonPage;
    }

    @Override
    public Map findByStatisticDate(String userId,Long statisticDate) {

        String dateTime = DateUtil.getDay(new Date(statisticDate));
        List<Object[]> list = userSettingPostureRepository.findByUserIdAndUploadTime(userId,dateTime);

        List<SettingPostureDTO> statistic = EntityUtils.castEntity(list, SettingPostureDTO.class, new SettingPostureDTO());

        Map map = new HashMap(2);
        //收集统计姿态信息
        List<JSONObject> content = new ArrayList<>();
        BigDecimal learningDuration = BigDecimal.ZERO;
        for (SettingPostureDTO dto : statistic){
            learningDuration = learningDuration.add(dto.getDurationTime());
        }
        for (SettingPostureDTO dto : statistic){
            JSONObject object = JSONObject.parseObject(JSON.toJSONString(dto));
            object.put("proportion",dto.getDurationTime().divide(learningDuration,2,BigDecimal.ROUND_HALF_UP));
            content.add(object);
        }

        map.put("content",content);
        //学习时长
        JSONObject statistics = new JSONObject();
        statistics.put("learningDuration",learningDuration);

        map.put("statistics",statistics);

        return map;
    }
}
